import {createRouter, createWebHashHistory} from 'vue-router'

//如果报红 npm i @types/nprogress -D 安装开发依赖声明
import np from 'nprogress'
import 'nprogress/nprogress.css'

//声明 module信息，解决ts meta:{title:''} 报红问题
declare module 'vue-router' {
    interface RouteMeta {
        title: string
    }
}

const router = createRouter({
    history: createWebHashHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            redirect: '/home'
        }, {
            path: '/home',
            name: 'home',
            meta: {
                title: '首页',
                icon: '',
            },
            component: () => import('../views/Home.vue'),
            redirect: '/home/welcome',
            children: [
                {
                    path: 'welcome',
                    name: 'welcome',
                    component: () => import('../views/Welcome.vue')
                },
            ]
        }, {
            path: '/login',
            name: 'login',
            meta: {
                title: '管理员登录',
                icon: '',
            },
            component: () => import('../views/Login.vue')
        }
    ]
})
np.configure({showSpinner: false})

//存储菜单
let m: any[] = [
    {
        path: '/emp',
        name: 'emp',
        meta: {
            title: '员工管理',
            icon: 'Menu',
            isshow: true,
        },
        component: 'Home',
        redirect: 'empmanager',
        children: [
            {
                path: '/emp/empmanager',
                name: 'empmanager',
                meta: {
                    title: '员工管理首页',
                    icon: 'Menu',
                    isshow: true,
                },
                component: 'empmanager'
            }, {
                path: '/emp/empadd',
                name: 'empadd',
                meta: {
                    title: '添加新员工',
                    icon: 'Menu',
                    isshow: true,
                },
                component: 'empadd'
            }
        ]
    }, {
        path: '/cai',
        name: 'cai',
        meta: {
            title: '分类管理',
            icon: 'Menu',
            isMenu: true, //代表就一个子菜单，直接显示此菜单，跳转到子菜单上
            isshow: true,
        },
        component: 'Home',
        redirect: '/cai/cm', //此处如果多个子菜单，写成 cm  如果只有一个子菜单，就写写/cai/cm
        children: [
            {
                path: '/cai/cm',
                name: 'cm',
                meta: {
                    title: '分类管理',
                    icon: 'Menu',
                    isshow: true,
                },
                component: 'category/CategoryManager'
            }
        ]
    }, {
        path: '/dish',
        name: 'dish',
        meta: {
            title: '菜品管理',
            icon: 'Menu',
            isshow: true,
        },
        component: 'Home',
        redirect: '/dish/dishmanager', //此处如果多个子菜单，写成 cm  如果只有一个子菜单，就写写/cai/cm
        children: [
            {
                path: '/dish/dishmanager',
                name: 'dishmanager',
                meta: {
                    title: '菜品管理',
                    icon: 'Menu',
                    isshow: true,
                },
                component: 'dish/DishManager'
            }, {
                path: '/dish/dishadd',
                name: 'dishadd',
                meta: {
                    title: '新增菜品',
                    icon: 'Menu',
                    isshow: true,
                },
                component: 'dish/DishAdd'
            },
        ]
    }, {
        path: '/home/cai3',
        name: 'cai3',
        meta: {
            title: '套餐管理',
            icon: 'Menu',
            isshow: true,
        },
        component: 'Cai',
    }, {
        path: '/home/cai4',
        name: 'cai4',
        meta: {
            title: '订单明细',
            icon: 'Menu',
            isshow: true,
        },
        component: 'Cai',
    }
]
localStorage.setItem("menu", JSON.stringify(m))
//
// const addChild = (parent: any, m: any) => {
//     let vs = import.meta.glob("/src/views/**/**.vue")
//     m.forEach((m: any) => {
//         let route = m
//         if (m.children && m.children.length > 0) {
//             //route = addChildren(route, m.children)
//             addChild(route, m.children)
//         } else {
//             let path = `/src/views/${m.component}.vue`
//             route.component = vs[path]
//         }
//         parent.children.push(route)
//     })
//     return parent;
// }
//
// const add = (m: any) => {
//     let vs = import.meta.glob("/src/views/**/**.vue")
//     m.forEach((m: any) => {
//         let route = m
//         if (m.children && m.children.length > 0) {
//             route = addChild(route, m.children)
//         }
//         let path = `/src/views/${m.component}.vue`
//         route.component = vs[path]
//         router.addRoute(route)
//     })
// }
//
// //将数组m 转换为路由并添加到router.add()
// add(m)

const ms = import.meta.glob('/src/views/**/*.vue')
const addChild = (parent: any, sub: any) => {
    parent.children = []
    sub.forEach((item: any): void => {
        let ss = item
        let path = `/src/views/${item.component}.vue`
        ss.component = ms[path]
        if (item.children) addChild(ss, item.children)
        parent.children.push(ss)
    })
    return parent
}
const addMenu = (menu: any) => {
    menu.forEach((item: any): void => {
        let obj = item
        let path = `/src/views/${item.component}.vue`
        obj.component = ms[path]
        if (item.children) {
            obj = addChild(obj, item.children)
        }
        router.addRoute(obj)
    })
}
addMenu(m)

router.beforeEach((to, from, next) => {
    np.start()

    if (to.path !== '/login') {
        if (!localStorage.getItem("token")) {
            router.replace("/login")
        }
    }

    document.title = to.meta.title
    //加载menun导航菜单信息
    let result = localStorage.getItem("menu")
    addMenu(JSON.parse(result || ''))

    next()
})

router.afterEach(() => {
    np.done()
})


export default router
